<?php
namespace App\Service\H5;

class ItemService extends BaseService
{
    //数据列表
    public static function index($pas)
    {
        $db = self::dbConn(self::$dbConfigItem)->where('del', 0);
        if (isset($pas['depart_id']) && $pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        $res = $db->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $leaderOne = (array) self::dbConn(self::$dbStaff)->where('id', $v->leader_id)->first();
            $leader_name = empty($leaderOne) ? '' : $leaderOne['title'];
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'starttime' => $v->starttime, 'endtime' => $v->endtime,
                'leader_id' => $v->leader_id, 'leader_name' => $leader_name,
            ];
        }
        return self::toback(0, '', $arys);
    }
    
    // 详情
    public static function getOne($id)
    {
        // 数据
        $res = (array) self::dbConn(self::$dbConfigItem)
            ->select('id', 'title', 'starttime', 'endtime', 'leader_id')
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            $res = [
                'id' => 0, 'title' => '',
                'starttime' => '', 'endtime' => '',
                'leader_id' => 0,
            ];
        }
        $arys['data'] = $res;
        // 负责人
        $arys['leader'] = self::dbConn(self::$dbStaff)
            ->select('id', 'title')
            ->where('is_able', 0)
            ->get();
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $res = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigItem)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbConfigItem)
            ->where('title', $pas['title'])
            ->where('id', '<>', $id)
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigItem)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbStDepart)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}